#!/usr/bin/python
# -*- coding:utf-8 -*-
# @FileName : DL4_test2.py
# Author    : myh

import torch
from torch import nn
from d2l import torch as d2l




def init_weights(m):
    if type(m) == nn.Linear:
        nn.init.kaiming_normal_(m.weight, a=0, mode='fan_in', nonlinearity='leaky_relu')


if __name__ == '__main__':
    net = nn.Sequential(nn.Flatten(),
                        nn.Linear(784, 256),
                        nn.ReLU(),
                        nn.Linear(256, 10))

    net.apply(init_weights)


    # 数据采集步长
    batch_size = 64
    train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)

    num_epochs, lr = 10, 0.1

    # 损失函数
    loss = nn.CrossEntropyLoss(reduction='none')
    trainer = torch.optim.SGD(net.parameters(), lr=lr)

    d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, trainer)
    d2l.plt.show()

    d2l.predict_ch3(net, test_iter)



